<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>通知</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../layui/css/layui.css"  media="all">
    <link rel="stylesheet" href="../../static/css/style.css"  media="all">
    <script type="text/javascript" src="../../static/js/jquery.min.js" ></script>
    <script src="../../layui/layui.js" charset="utf-8"></script>
    <script src="../../static/js/base.js" charset="utf-8"></script>
    <script src="../../static/js/ax.js"></script>
    <script src="../../static/js/jquery.cookie.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="../../static/js/html5.js"></script>
    <script src="../../static/js/respond.js"></script>
    <![endif]-->
</head>
<body class="bg_w">

    <div class="content-wrap">

        <div class="layui-row">
            <form class="layui-form layui-form-pane" id="from1">
                <input type="hidden" id="dataId" value="" />
                <div class="layui-row">
                    <div class="layui-form-item layui-col-sm6">
                        <label class="layui-form-label">标题</label>
                        <div class="layui-input-block">
                            <input type="text" id="noticeTitle" value="" class="layui-input" lay-verify="required" maxlength="100" placeholder="请输入标题">
                        </div>
                    </div>
                    <div class="layui-col-sm6">
                        <h5 class="lheight"><span class="red">*</span>100字符内</h5>
                    </div>
                </div>


                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">内容</label>
                    <div class="layui-input-block" style="background: #fff">
                        <textarea id="noticeContent" style="display: none;"></textarea>
                    </div>
                </div>

                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">接收人</label>
                    <div class="layui-input-block">
                        <div class="layui-textarea dispenseOrgList">
                            <!--001-渝北驾校,002-渝北驾校-->
                        </div>
                        <span class="layui-layout-right dispenseOrgCount" style="bottom: 10px;right:10px;top: auto">0个驾校</span>
                    </div>
                </div>



                <div class="layui-form-item layui-col-space10">
                    <div class="layui-form-item" >
                        <label class="layui-form-label ">弹窗时间</label>
                        <div class="layui-input-block">
                            <input type="text" class="layui-input" id="tipsStartTime" autocomplete="off" style="float: left;width:46%;"><span style="float: left; width: 8%;text-align: center; line-height: 36px;">～</span>
                            <input type="text" class="layui-input" id="tipsEndTime" autocomplete="off" style="float: left;width: 46%;">
                        </div>
                    </div>

                </div>


                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button type="button" id="btnClose" class="layui-btn layui-btn-primary btn-left">关闭</button>
                        <button type="button" id="btnSubmit" class="layui-btn add_btn btn-left">提交</button>
                    </div>
                </div>

            </form>
        </div>
    </div>

</body>

<script>

    var orgList = [];
    var $;
    var layedit;
    layui.use(['layedit','form','laydate','layer'], function() {
        $ = layui.$;
        layedit = layui.layedit;
        var form = layui.form,
            layDateTipsStartTime = layui.laydate,
            layDateTipsEndTime = layui.laydate,
            layer=layui.layer;

        // 初始化富文本编辑器
        layedit.set({
            uploadImage: {
                url: fileServiceUrl + systemhl +  '/file/uploadByLayUI?' + ax.getParamStr() //接口url
            }
        });
        var noticeContentIndex = layedit.build('noticeContent',{
        //     uploadImage: {
        //         url: fileServiceUrl+'/file/uploadByLayUI?type=1&appId=111&userId=11' //接口url
        //         ,type: 'post' //默认post
        //         // ,data:JSON.stringify(uploadParam)
        //     }
        //
        }); //建立编辑器

        // 初始化日期控件
        layDateTipsStartTime.render({
            elem: '#tipsStartTime',
            type:"datetime"

        });
        layDateTipsEndTime.render({
            elem: '#tipsEndTime',
            type:"datetime"
        });

        $('#btnClose').on("click", function () {
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        });

        $('#btnSubmit').on('click', function(){
            var noticeData = {
                id:$("#dataId").val(),
                noticeTitle:$("#noticeTitle").val().trim(),
                noticeContent:layedit.getContent(noticeContentIndex),
                tipsStartTime:$("#tipsStartTime").val(),
                tipsEndTime:$("#tipsEndTime").val(),
                dispenseOrgList:orgList
            };
            if(checkData(noticeData)) {
                saveData(noticeData);
            }
        });

        $('.dispenseOrgList').on('click', function () {

            layer.open({
                type: 2,
                area: ['90%', '80%'],
                fixed: true, //不固定
                maxmin: true,
                content: '../common/selectDriverSchool.html?isBatch=1'
            });
        });

        var initPage = function(dataId) {
            ax.get({
                url: backendWebUrl + "/api/notice/detail",
                data: {dataId:dataId},
                success: function (res) {
                    if (res.code == 0) {
                        loadPage(res.data);
                    } else {
                        layer.msg(res.message);
                    }
                }

            });
        };
        var loadPage = function (pageData) {
            $("#noticeTitle").val(pageData.noticeData.noticeTitle);
            layedit.setContent(noticeContentIndex, pageData.noticeData.noticeContent, false);
            $("#tipsStartTime").val(pageData.noticeData.tipsStartTime);
            $("#tipsEndTime").val(pageData.noticeData.tipsEndTime);
            // 加载下发驾校列表
            orgList = pageData.dispenseOrgList;
            loadDispenseOrg();
        };
        // 编辑初始化数据
        var dataId = getQueryString("id");
        $("#dataId").val(dataId);
        if(dataId != null && dataId != "") {
            // 有ID，为编辑
            initPage(dataId);
        }
    });

    function loadDispenseOrg() {
        var orgStr = "";
        $.each(orgList, function (index, orgObj) {
            if(orgStr.length > 0) {
                orgStr += ",";
            }
            orgStr += orgObj.orgCode + "-" + orgObj.orgName;
        });

        $(".dispenseOrgList").text(orgStr);
        $(".dispenseOrgCount").text(orgList.length + "个驾校");
    }

    function checkData(noticeData) {
        var checkResult = true;
        if(noticeData.noticeTitle.length == 0) {
            layer.msg("通知标题不能为空");
            checkResult = false;
        } else if(noticeData.noticeTitle.length > 100) {
            layer.msg("通知标题应在100字符内");
            checkResult = false;
        } else if(noticeData.noticeContent.length == 0) {
            layer.msg("通知内容不能为空");
            checkResult = false;
        } else if(noticeData.noticeContent.length > 5000) {
            layer.msg("通知内容应在5000字符内");
            checkResult = false;
        } else if(noticeData.dispenseOrgList.length == 0) {
            layer.msg("请选择接收人");
            checkResult = false;
        } else if(noticeData.dispenseOrgList.length > 16) {
            layer.msg("接收人不能超过16个");
            checkResult = false;
        }
        return checkResult;
    }
    function saveData(noticeData) {

        var dataId = $("#dataId").val();
        if(dataId != null && dataId != "") {
            ax.post({
                url: backendWebUrl + "/api/notice/update",
                data: JSON.stringify(noticeData),
                success: function (res) {
                    if (res.code == 0) {
                        topFrame.location.reload();
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                    } else {
                        layer.msg(res.message);
                    }
                }

            });
        } else {
            ax.post({
                url: backendWebUrl + "/api/notice/add",
                data: JSON.stringify(noticeData),
                success: function (res) {
                    if (res.code == 0) {
                        topFrame.location.reload();
                        parent.layer.close(parent.layer.getFrameIndex(window.name));
                    } else {
                        layer.msg(res.message);
                    }
                }

            });
        }
    }
    
    function selectDriverSchool(dsList) {
        orgList = [];
        $.each(dsList, function (index, ds) {
            orgList.push({
                orgId:ds.id,
                orgCode:ds.code,
                orgName:ds.name
            });
        });
        loadDispenseOrg();
    }
</script>
</html>